package org.deng.learn.designpattern.flyweight;

import java.util.HashMap;
import java.util.Map;

/**
 * @author DengLibin
 * @Description 字体对象工厂
 * @Date 2019/3/7 0007 17:39
 */
public class FontFactory {

    private Map<String, Font> map = new HashMap<>();

    private  static  class Inner{
        static {
            System.out.println("内部类静态代码块");
        }
        private  static  FontFactory fontFactory = new FontFactory();
    }
    private FontFactory(){}
    public static  FontFactory getInstance(){
        return Inner.fontFactory;
    }

    public  Font getFont(String name){
        Font font = map.get(name); //从对象池中获取
        if(font == null){
            synchronized(FontFactory.class){
                if((font =map.get(name)) == null){
                    font = new Font(name);
                    map.put(name, font);
                    return font;
                }
                return font;
            }
        }else{
            return font;
        }
    }
}
